﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using WF_QLThuVien.Entities.PhieuMuon;
using WF_QLThuVien.BusinessLayer.DauSach;
using WF_QLThuVien.BusinessLayer.QuyDinh;
using WF_QLThuVien.Entities.QuyDinh;
using WF_QLThuVien.Entities;
using WF_QLThuVien.BusinessLayer;

namespace WF_QLThuVien.PresentationLayer
{
    public partial class FormBaoMatSach : Form
    {
        private ArrayList m_dataPM;
        CDocGia GDocGia;
        CDocGiaBLL GDocGiaBLL = new CDocGiaBLL();

        private String m_maSach = "";
        #region: Sách. PANDA
        CBLL_DauSach GDauSachBLL = new CBLL_DauSach();
        #endregion
        CQuyDinh GQuyDinh;
        CBLL_QuyDinh GQuyDinhBLL = new CBLL_QuyDinh();
        #region: Quy Định. PANDA
        
        #endregion


        #region: Delegate
        public delegate void UpdateClickBaoMatSach();
        public event UpdateClickBaoMatSach UpdateData_Click;
        #endregion
        public FormBaoMatSach()
        {
            InitializeComponent();
        }

        public FormBaoMatSach(ArrayList dataPM)
        {
            InitializeComponent();
            m_dataPM = dataPM;
        }

        private void btnTimSach_Click(object sender, EventArgs e)
        {
            dgvChiTietSach.Refresh();
            string maDocGia = txtMaDocGia.Text;
            GDocGia = GDocGiaBLL.timMaDocGia(maDocGia);
            if (GDocGia.DocGiaID == "")
            {
                MessageBox.Show("Nhập sai mã đọc giả!");
                return;
            }
            else
            {
                ArrayList dsSach = new ArrayList();
                foreach (CPhieuMuon pm in m_dataPM)
                {
                    if (pm.DocGiaID.ToString().Equals(maDocGia))
                    {
                        foreach (CCTPhieuMuon ctpm in pm.CtPhieuMuon)
                        {
                            if (ctpm.TrangThai.ToString().Equals(@"Dang muon") && ctpm.Sach.TrangThai.ToString().Equals(@"Dang muon"))
                            {
                                dsSach.Add(ctpm.Sach);
                            }
                        }
                    }
                }
                if (dsSach.Count == 0)
                {
                    MessageBox.Show("Không Có Sách Đang Mượn!");
                }
                else
                {
                    dgvChiTietSach.DataSource = dsSach;
                }
            }
            
        }

        private void dgvChiTietSach_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvChiTietSach.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
            {
                m_maSach = dgvChiTietSach.Rows[dgvChiTietSach.CurrentRow.Index].Cells["SachID"].Value.ToString(); //Láy biến Global mã Sách, dùng cho btnBaoMat
                string maDauSach = dgvChiTietSach.Rows[dgvChiTietSach.CurrentRow.Index].Cells["DauSachID"].Value.ToString();
                string trangThai = dgvChiTietSach.Rows[dgvChiTietSach.CurrentRow.Index].Cells["TrangThai"].Value.ToString();
                string ghiChu = dgvChiTietSach.Rows[dgvChiTietSach.CurrentRow.Index].Cells["GhiChu"].Value.ToString();
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnBaoMat_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_maSach != "")
                {
                    //Update trạng thái sách và tính tiền
                    if (GDauSachBLL.UpdateTTMatSach(m_maSach))
                    {
                        MessageBox.Show("Tiền Phạt Mất Sách Theo Quy Định: " + GDauSachBLL.GiaTienMatSach(m_maSach).ToString());
                        UpdateData_Click(); 
                        this.Dispose();
                    }
                }
                else
                {
                    MessageBox.Show("Chọn Sách cần Báo Mất!");
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void FormBaoMatSach_Load(object sender, EventArgs e)
        {
            GQuyDinh = GQuyDinhBLL.GetQuyDinh();
        }
    }
}
